﻿

CREATE FUNCTION [dbo].[GetArticolAliniat] (@Cont TipCont)  
RETURNS TipCont  AS  
BEGIN 
DECLARE @ret TipCont
SET @ret = ''	
IF LEN(@Cont) >=22
	BEGIN
		SET @ret = SUBSTRING(@Cont	,17,6)
		IF SUBSTRING(@ret,5,2)='00'
			SET @ret = SUBSTRING(@ret,1,4)
	END 
ELSE
	BEGIN
		DECLARE @posPunct int
		SET @posPunct = CHARINDEX('.',@Cont)
		SET @ret = ''
		IF @posPunct <> 0
			BEGIN
				SET @ret = SUBSTRING(@Cont	,@posPunct+1,LEN(@Cont)-@posPunct)
			END
	END		
return @ret	
END